﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XMM.Controls.Library
{
    /// <summary>
    /// Interaktionslogik für Details_Movie.xaml
    /// </summary>
    public partial class Details_Movie : UserControl
    {
        /// <summary>
        /// Database-ID of the selected movie.
        /// </summary>
        public int SelectedMovieID { get; set; }

        /// <summary>
        /// Initialize a new Movie-Details-Control for the given Movie-Database-ID.
        /// </summary>
        /// <param name="iTargetMovieID">(int) Database-ID of the Movie to load upon initialization.</param>
        public Details_Movie(int iTargetMovieID)
        {
            InitializeComponent();

            SelectedMovieID = iTargetMovieID;
        }

        // Controls
        StackPanel spnAudioQuality;


        /// <summary>
        /// Load Movie-Data and update fields.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            blankValues();

            Objects.Movie oSelectedMovie = new XMM.Objects.Movie(SelectedMovieID);

            if (oSelectedMovie != null && oSelectedMovie.MovieID != 0)
            {
                /*********************************************************************
                 * Get Base-Movie-Data
                 * *******************************************************************/
                lblMovieTitle.Content = oSelectedMovie.Name;
                lblSubtitle.Content = oSelectedMovie.Subtitle;
                lblGenre.Content = new Objects.Genre(oSelectedMovie.GenreID).Name;
                lblDirector.Content = oSelectedMovie.Director;
                txbCast.Text = oSelectedMovie.Cast;
                txbPlot.Text = oSelectedMovie.Plot;

                string sCoverPath = new Settings().LibraryDataStorage + "movies\\images\\xmm_" + Tools.getFileName(oSelectedMovie.Files[0]) + ".jpg";
                if (System.IO.File.Exists(sCoverPath))
                {
                    imgCover.Source = new BitmapImage(new Uri(sCoverPath, UriKind.Absolute));
                    imgCover.Stretch = Stretch.Uniform;
                }

                /*********************************************************************
                 * Get Audio-Info
                 * *******************************************************************/
                if (spnAudio != null && spnAudio.Children.Count == 3)
                {
                    spnAudio.Children.RemoveAt(2);
                    spnAudio.Children.RemoveAt(1);
                }

                if (oSelectedMovie.AudioInfo != null)
                {
                    spnAudioQuality = new StackPanel();
                    spnAudioQuality.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    spnAudioQuality.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    spnAudioQuality.Orientation = Orientation.Horizontal;

                    Image imgMaxChannels = new Image();
                    imgMaxChannels.Width = imgMaxChannels.Height = 24;
                    imgMaxChannels.Stretch = Stretch.Uniform;
                    imgMaxChannels.Source = new BitmapImage(new Uri(@"../../images/bin/channels_64.png", UriKind.Relative));
                    spnAudioQuality.Children.Add(imgMaxChannels);

                    // Get max channels.
                    Label lblMaxChannels = new Label();
                    lblMaxChannels.Content = oSelectedMovie.AudioInfo.Channels.ToString();
                    spnAudioQuality.Children.Add(lblMaxChannels);

                    // Get best audio quality for representation.
                    Image imgAudioQuality = new Image();
                    imgAudioQuality.Width = imgAudioQuality.Height = 24;
                    imgAudioQuality.Stretch = Stretch.Uniform;

                    if (oSelectedMovie.AudioInfo.IsDolbyDigital)
                    {
                        imgAudioQuality.Source = new BitmapImage(new Uri(@"../../images/bin/dd.png", UriKind.Relative));                        
                        spnAudioQuality.Children.Add(imgAudioQuality);
                    }
                    else if (oSelectedMovie.AudioInfo.IsDolbySurround)
                    {
                        imgAudioQuality.Source = new BitmapImage(new Uri(@"../../images/bin/dolby.png", UriKind.Relative));
                        spnAudioQuality.Children.Add(imgAudioQuality);
                    }
                    else if (oSelectedMovie.AudioInfo.IsDTS)
                    {
                        imgAudioQuality.Source = new BitmapImage(new Uri(@"../../images/bin/dts.png", UriKind.Relative));
                        spnAudioQuality.Children.Add(imgAudioQuality);
                    }

                    // Get total audio tracks...
                    StackPanel spnTracks = new StackPanel();
                    spnTracks.Orientation = Orientation.Horizontal;

                    Image imgAudioTracks = new Image();
                    imgAudioTracks.Width = imgAudioTracks.Height = 24;
                    imgAudioTracks.Stretch = Stretch.Uniform;
                    imgAudioTracks.Source = new BitmapImage(new Uri(@"../../images/bin/tracks.png", UriKind.Relative));
                    spnTracks.Children.Add(imgAudioTracks);

                    Label lblAudioTracks = new Label();
                    lblAudioTracks.Content = oSelectedMovie.AudioInfo.Tracks.ToString();
                    spnTracks.Children.Add(lblAudioTracks);

                    // add audio-tracks to gui.
                    spnAudioQuality.Children.Add(spnTracks);

                    // Add audio-quality info to gui.
                    spnAudio.Children.Add(spnAudioQuality);

                    // Get audio-languages...
                    StackPanel spnAudioLanguages = new StackPanel();
                    spnAudioLanguages.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    spnAudioLanguages.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    spnAudioLanguages.Orientation = Orientation.Horizontal;

                    int iCurrentLanguage = 0;
                    foreach (string sAudioLanguage in oSelectedMovie.AudioInfo.Languages)
                    {
                        Image imgLanguage = new Image();
                        imgLanguage.Width = imgLanguage.Height = 24;
                        imgLanguage.Stretch = Stretch.Uniform;
                        string sImagePath = Tools.getInstallationPath() + "images\\lang\\" + Functions.LanguageTools.getNormalizedLanguage(sAudioLanguage) + ".png";
                        if (System.IO.File.Exists(sImagePath))
                        {
                            imgLanguage.Source = new BitmapImage(new Uri(@"../../images/lang/" + Functions.LanguageTools.getNormalizedLanguage(sAudioLanguage) + ".png", UriKind.Relative));
                            imgLanguage.ToolTip = sAudioLanguage + " (Track: " + iCurrentLanguage.ToString() + "; Codec: " + oSelectedMovie.AudioInfo.AudioCodecs[iCurrentLanguage] + ")";
                        }
                        else
                        {
                            imgLanguage.Source = new BitmapImage(new Uri(@"../../images/bin/unknown.png", UriKind.Relative));
                            imgLanguage.ToolTip = "Unbekannte Sprache \"" + sAudioLanguage + "\" (Track: " + iCurrentLanguage.ToString() + "; Codec: " + oSelectedMovie.AudioInfo.AudioCodecs[iCurrentLanguage] + ")";
                        }
                        spnAudioLanguages.Children.Add(imgLanguage);
                        ++iCurrentLanguage;
                    }

                    spnAudio.Children.Add(spnAudioLanguages);
                }

                /*********************************************************************
                 * Get Video-Info
                 * *******************************************************************/
                if (spnVideo != null && spnVideo.Children.Count == 2)
                {
                    spnVideo.Children.RemoveAt(1);
                }

                if (oSelectedMovie.VideoInfo != null)
                {
                    StackPanel spnVideoQuality = new StackPanel();
                    spnVideoQuality.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    spnVideoQuality.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    spnVideoQuality.Orientation = Orientation.Horizontal;

                    // Get Video-Tracks.
                    Image imgVideoTracks = new Image();
                    imgVideoTracks.Width = imgVideoTracks.Height = 24;
                    imgVideoTracks.Source = new BitmapImage(new Uri(@"../../images/bin/tracks.png", UriKind.Relative));
                    imgVideoTracks.ToolTip = oSelectedMovie.VideoInfo.Codec;
                    spnVideoQuality.Children.Add(imgVideoTracks);

                    Label lblVideoTracks = new Label();
                    lblVideoTracks.Content = oSelectedMovie.VideoInfo.VideoTracks.ToString();
                    lblVideoTracks.ToolTip = oSelectedMovie.VideoInfo.Codec;
                    spnVideoQuality.Children.Add(lblVideoTracks);

                    // Get Quality from HD-Level.
                    Image imgVideoQuality = new Image();
                    imgVideoQuality.Width = imgVideoQuality.Height = 32;
                    imgVideoQuality.Stretch = Stretch.Uniform;

                    // Assign HD-Level-Image.
                    switch (oSelectedMovie.VideoInfo.HDLevel)
                    {
                        case 0:
                            imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/sd.png", UriKind.Relative));
                            break;
                        case 1:
                            imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/hd720.png", UriKind.Relative));
                            break;
                        case 2:
                            imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/hd1080i.png", UriKind.Relative));
                            break;
                        case 3:
                            imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/hd1080.png", UriKind.Relative));
                            break;
                    }

                    spnVideoQuality.Children.Add(imgVideoQuality);

                    // Add quality-info to GUI.
                    spnVideo.Children.Add(spnVideoQuality);

                    // Get Subtitle-Languages.
                    StackPanel spnSubtitles = new StackPanel();
                    spnSubtitles.Orientation = Orientation.Horizontal;
                    spnSubtitles.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    spnSubtitles.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

                    int iCurrentSubtitleTrack = 0;

                    if (oSelectedMovie.VideoInfo.SubtitleTracks > 0)
                    {
                        foreach (string sLanguage in oSelectedMovie.VideoInfo.SubtitleLanguages)
                        {
                            Image imgSubtitle = new Image();
                            imgSubtitle.Stretch = Stretch.Uniform;
                            imgSubtitle.Width = imgSubtitle.Height = 24;

                            string sLanguageImgPath = Tools.getInstallationPath() + "images\\lang\\" + Functions.LanguageTools.getNormalizedLanguage(sLanguage) + ".png";
                            if (System.IO.File.Exists(sLanguageImgPath))
                            {
                                imgSubtitle.Source = new BitmapImage(new Uri(@"../../images/lang/" + Functions.LanguageTools.getNormalizedLanguage(sLanguage) + ".png", UriKind.Relative));
                                imgSubtitle.ToolTip = sLanguage + " (Track: " + iCurrentSubtitleTrack.ToString() + ")";
                            }
                            else
                            {
                                imgSubtitle.Source = new BitmapImage(new Uri(@"../../images/bin/unknown.png", UriKind.Relative));
                                imgSubtitle.ToolTip = "Unbekannte Sprache \"" + sLanguage + "\" (Track: " + iCurrentSubtitleTrack.ToString() + ")";
                            }

                            spnSubtitles.Children.Add(imgSubtitle);
                            ++iCurrentSubtitleTrack;
                        }
                        spnVideo.Children.Add(spnSubtitles);
                    }

                }


                /*********************************************************************
                 * Get File-Info
                 * *******************************************************************/
                if (spnFiles != null && spnFiles.Children.Count > 1)
                {
                    for (int i = spnFiles.Children.Count - 1; i > 0; i--)
                    {
                        spnFiles.Children.RemoveAt(i);
                    }
                }

                // Get DiskMedia-Info.
                StackPanel spnMediaInfo = new StackPanel();
                spnMediaInfo.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                spnMediaInfo.Orientation = Orientation.Horizontal;

                Image imgDiskMedia = new Image();
                imgDiskMedia.Stretch = Stretch.Uniform;
                imgDiskMedia.Width = imgDiskMedia.Height = 24;
                Label lblDiskMedia = new Label();

                if (oSelectedMovie.DiskMediaID != 0)
                {
                    Objects.DiskMedia oMedia = new Objects.DiskMedia(oSelectedMovie.DiskMediaID);

                    imgDiskMedia.Source = new BitmapImage(new Uri(@"../../images/bin/disk.png", UriKind.Relative));
                    imgDiskMedia.ToolTip = oMedia.MediaName + " (Medium hinzugefügt: " + oMedia.MediaDate.ToShortDateString() + ")";
                    lblDiskMedia.Content = oMedia.MediaName;
                }
                else
                {
                    imgDiskMedia.Source = new BitmapImage(new Uri(@"../../images/bin/cdrom_64.png", UriKind.Relative));
                    imgDiskMedia.ToolTip = lblDiskMedia.Content = "Separater Datenträger";
                }

                spnMediaInfo.Children.Add(imgDiskMedia);
                spnMediaInfo.Children.Add(lblDiskMedia);
                spnFiles.Children.Add(spnMediaInfo);

                // Get Files.
                StackPanel spnFileList = new StackPanel();
                spnFileList.Orientation = Orientation.Vertical;
                spnFileList.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                spnFileList.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

                foreach (string sFilePath in oSelectedMovie.Files)
                {
                    if (sFilePath != String.Empty)
                    {
                        Label lblFileInfo = new Label();
                        lblFileInfo.Content = "•  " + sFilePath;

                        if (System.IO.File.Exists(sFilePath))
                            lblFileInfo.Foreground = Brushes.DarkGreen;
                        else
                            lblFileInfo.Foreground = Brushes.Red;

                        spnFileList.Children.Add(lblFileInfo);
                    }
                }

                spnFiles.Children.Add(spnFileList);
            }
        }

        private void blankValues()
        {
            lblMovieTitle.Content = "Nicht verfügbar";
            lblSubtitle.Content = "Nicht verfügbar";
            lblGenre.Content = "Nicht verfügbar";
            lblDirector.Content = "Nicht verfügbar";
            txbCast.Text = "Nicht verfügbar";
            txbPlot.Text = "Nicht verfügbar";
            imgCover.Source = null;
        }
    }
}
